<?php


namespace app\system\controller;


use app\common\model\AdminModel;
use app\common\model\cms\DetailModel;
use helper\Qrcode;
use think\facade\Cookie;

class PublicController extends BaseController
{

  /**
   * 不需要登陆的方法
   * @var array
   */
  protected $noLogin = ['login'];

  /**
   * 不检测权限方法
   * @var array
   */
  protected $noCheck = [];


  protected $model;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model = new AdminModel();
  }

  /**
   * 用户登录
   * @return string
   * @throws \Exception
   */
  public function login()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
        'account|登录账号' => 'require',
        'password|登录密码' => 'require',
        'captcha_code|验证码' => 'require|captcha',
      ]);
      $token = $this->model->login($data['account'], $data['password']);
      Cookie::forever('system_token', $token);
      $this->success('登录成功');
    }
    return $this->fetch();
  }
  /*二维码*/
  public function qrcode(){
    $text = $this->request->param('text');
    return Qrcode::img(html_out($text));
  }

  /*获取产品select*/
  public function selectProduct(){
    $params = $this->getSelectPageQuery();
    $model  = new DetailModel();
    $total  = $model->setSuffix('product')->where($params['map'])->count();
    $list   = [];
    if ($total > 0) {
      $fields = request()->param('fields', true);
      $list   = $model->setSuffix('product')->field($fields)->where($params['map'])
          ->order($params['order'])
          ->page($params['pageNumber'], $params['pageSize'])
          ->select()->toArray();
    }
    $data['totalRow'] = $total;
    $data['list']     = $list;
    return  json($data);
  }
}
